﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Freebase4net.Tests
{
    [TestClass]
    public class ImageServiceTests
    {
        private ImageService _imageService;
        private string _id;

        public ImageServiceTests()
        {
            _imageService = FreebaseServices.CreateImageService();
            _id = "/en/the_animal";
        }

        [TestMethod]
        public void GetImageUrl_Id()
        {
            string result = _imageService.GetImageUrl(_id);


            Assert.AreEqual(BaseString(), result);
        }

        [TestMethod]
        public void GetImageUrl_IdAndMaxWidth()
        {
            string result = _imageService.GetImageUrl(_id, maxwidth: "150");


            Assert.AreEqual(string.Format("{0}{1}", BaseString(), "maxwidth=150&"), result);
        }

        [TestMethod]
        public void GetImageUrl_IdAndMaxWidthAndMaxHeight()
        {
            string result = _imageService.GetImageUrl(_id, maxwidth: "150", maxheight: "150");


            Assert.AreEqual(string.Format("{0}{1}{2}", BaseString(), "maxheight=150&", "maxwidth=150&"), result);
        }

        [TestMethod]
        public void GetImageUrl_IdAndMaxWidthAndMaxHeightAndPad()
        {
            string result = _imageService.GetImageUrl(_id, maxwidth: "150", maxheight: "150", pad: true);


            Assert.AreEqual(string.Format("{0}{1}{2}{3}", BaseString(), "maxheight=150&", "maxwidth=150&", "pad=1&"), result);
        }

        [TestMethod]
        public async Task SaveImage_IdAndFileName_ShouldReturnFalse()
        {
            var result = await _imageService.SaveImageAsync(_id, "file.png");


            Assert.AreEqual(true, result);
        }

        [TestMethod]
        public async Task SaveImage_IfImageExist_ShouldReturnFalse()
        {
            var result = await _imageService.SaveImageAsync(_id, "file.png", true);


            Assert.AreEqual(false, result);
        }

        [TestMethod]
        public async Task CheckIfImageExist_Id_ShouldReturnFalse()
        {
            var result = await _imageService.CheckIfImageExistAsync(_id);


            Assert.AreEqual(false, result);
        }

        [TestMethod]
        public async Task DownloadImage_Id_ShouldNotNullStream()
        {
            var result = await _imageService.DownloadImageAsync(_id);


            Assert.IsNotNull(result);
        }

        private string BaseString()
        {
            return string.Format("https://usercontent.googleapis.com/freebase/v1/image{0}?key=AIzaSyC9N5HdZl15OjRcuOFxZ1SwngjCxIebbYM&", _id);
        }
    }
}
